/****************************************************************************
 * 2025.8 楠
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using ProjectSurvivor;
using QAssetBundle;
using UnityEngine.U2D;

namespace QFrameworkSurvivor
{
    public partial class ExpUpgradePanel : UIElement, IController
    {
        ResLoader mResloader;
        private void Awake()
        {
            mResloader = ResLoader.Allocate();
            var iconAtlas = mResloader.LoadSync<SpriteAtlas>("icon");

            var expUpgradeSystem = this.GetSystem<ExpUpgradeSystem>();

            foreach (var expUpgradeitem in expUpgradeSystem.Items)
            {
                BtnExpUpgradeltemTemplate.InstantiateWithParent(UpgradeRoot)
                    .Self(self =>
                    {
                        var item = expUpgradeitem;
                        self.transform.Find("Icon").GetComponent<Image>().sprite = iconAtlas.GetSprite(item.IconName);
                        self.onClick.AddListener(() =>
                        {
                            AudioKit.PlaySound(Sfx.BTNCLICK);
                            Time.timeScale = 1.0f;
                            item.Upgrade();
                            this.Hide();
                        });
                        var selfCache = self;
                        item.Visible.RegisterWithInitValue(visiblelaol =>
                        {
                            if (visiblelaol)
                            {
                                selfCache.Show();
                                var pairedUpgradeName = selfCache.transform.Find("PairedUpgradeName");
                                if (expUpgradeSystem.Pairs.TryGetValue(item.Key, out var pairedName))
                                {
                                    var pairedItem = expUpgradeSystem.Dictionary[pairedName];
                                    if (pairedItem.Currentlevel.Value > 0 && item.Currentlevel.Value == 0)
                                    {

                                        //pairedUpgradeName.GetComponent<Text>().text = "配对技能" + pairedItem.Key;
                                        pairedUpgradeName.Show();
                                        pairedUpgradeName.Find("Icon").GetComponent<Image>().sprite = iconAtlas.GetSprite(pairedItem.IconName);
                                    }
                                    else
                                    {
                                        pairedUpgradeName.Hide();
                                    }
                                }
                                else
                                {
                                    pairedUpgradeName.Hide();
                                }
                            }
                            else
                            {
                                selfCache.Hide();
                            }
                        }).UnRegisterWhenGameObjectDestroyed(selfCache);
                        item.Currentlevel.RegisterWithInitValue(level =>
                        {
                            selfCache.GetComponentInChildren<Text>().text = item.Description;
                        }).UnRegisterWhenGameObjectDestroyed(selfCache);
                    });
            }
        }

        protected override void OnBeforeDestroy()
        {
            mResloader.Recycle2Cache();
            mResloader = null;
        }

        public IArchitecture GetArchitecture()
        {
            return Global.Interface;
        }
    }
}